﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using Meqs.Entity;
using Meqs.Logic;

public partial class Student_Report_HistoryPosition : System.Web.UI.Page
{
    protected List<StuExam> stuExam = new List<StuExam>();
    protected List<int> schoolYear = new List<int>();
    /// <summary>
    /// 本次考试的科目(统考)
    /// </summary>
    protected IList<ExamSubjecyEducation2> subjEdu = null;
    /// <summary>
    /// 本次考试的科目(校考)
    /// </summary>
    protected IList<ExamSubjecySchool2> subjSchool = null;
    /// <summary>
    /// 统考成绩列表（已经输入的）
    /// </summary>
    protected IList<GradeScoreEducation> gse = null;
    /// <summary>
    /// 校考试成绩
    /// </summary>
    protected IList<ExamDetailSchool> eds = null;
    /// <summary>
    /// 最大的科目
    /// </summary>
    protected List<ExamSubjecyEducation2> temp;
    protected List<HistoryPosition> hps;
    protected List<GlobalCauseInfo> causeinfo;
    protected int isubjID=0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string eduIdss = "0";
            string schoolIdss = "0";
            Hashtable ht = null;
            for (int i = 1; i < 13; i++)
            {
                ht = new Hashtable();
                ht.Add("StudentID", CommonScore.LogonStudent.RID);
                if(!string.IsNullOrEmpty(CommonScore.GetGradeStoreTable(i)))
                {
                    ht.Add("TableName", CommonScore.GetGradeStoreTable(i));
                    IList<ExamsEducation> eus = Container<Meqs.Logic.Interface.IExamsEducationLogic>.Instance.FindStudentExam(ht);
                    foreach (ExamsEducation u in eus)
                    {
                        StuExam st = new StuExam(1, u.RID, u.Name, u.SchoolYear, u.Grade, u.ClassType,u.CreateDate);
                        stuExam.Add(st);
                        eduIdss += "," + u.RID;
                    }
                }
            }

            ht = new Hashtable();
            ht.Add("StudentID", CommonScore.LogonStudent.RID);
            ht.Add("SchoolMark", CommonScore.GetSchoolMark);
            IList<ExamsSchool> ess = Container<Meqs.Logic.Interface.IExamsSchoolLogic>.Instance.FindStudentExam(ht);
            
            foreach (ExamsSchool u in ess)
            {
                StuExam st = new StuExam(0, u.RID, u.Name, u.SchoolYear, u.Grade, u.ClassType, u.CreateDate);
                schoolIdss += "," + u.RID;
                stuExam.Add(st);
            }

            foreach (StuExam se in stuExam)
            {
                if (!schoolYear.Contains(se.schoolYear))
                {
                    int index = 0;
                    foreach (int y in schoolYear)
                    {
                        if (se.schoolYear > y)
                        {
                            break;
                        }
                        index++;
                    }
                    schoolYear.Insert(index, se.schoolYear);
                }
            }

            ht = new Hashtable();
            ht.Add("SubjectIDS", eduIdss);
            subjEdu = Container<Meqs.Logic.Interface.IExamSubjecyEducationLogic>.Instance.GetExamCause(ht);
            ht = new Hashtable();
            ht.Add("SchoolMark", CommonScore.GetSchoolMark);
            ht.Add("SubjectIDS", schoolIdss);
            subjSchool = Container<Meqs.Logic.Interface.IExamSubjecySchoolLogic>.Instance.GetExamCause(ht);
            causeinfo = new List<GlobalCauseInfo>();

            foreach (ExamSubjecyEducation2 ee in subjEdu)
            {
                if (!Check(causeinfo, ee.SubjectID))
                {
                    GlobalCauseInfo gci = new GlobalCauseInfo();
                    gci.RID = ee.SubjectID;
                    gci.CauseName = ee.SubjectName;
                    causeinfo.Add(gci);
                }
            }

            foreach (ExamSubjecySchool2 ee in subjSchool)
            {
                if (!Check(causeinfo, ee.SubjectID))
                {
                    GlobalCauseInfo gci = new GlobalCauseInfo();
                    gci.RID = ee.SubjectID;
                    gci.CauseName = ee.SubjectName;
                    causeinfo.Add(gci);
                }
            }
          
        }
    }

    private bool Check(List<GlobalCauseInfo> temp, int subjid)
    {
        foreach (GlobalCauseInfo t in temp)
        {
            if (t.RID == subjid)
            {
                return true;
            }
        }
        return false;
    }

    protected void btnBind_Click(object sender, EventArgs e)
    {
        string starty = txtStart.Text;
        string starte = txtEnd.Text;
        string eduids = txteduIDS.Text;
        string edugrades = txteduGrades.Text;
        string schoolids = txtschoolIDS.Text;
        string subjID = txtSubjID.Text;
        if (subjID.Length<=0)
        {
            return;
        }
        hps = new List<HistoryPosition>();
        hps.Add(new HistoryPosition("班级排名",new List<string>(),new List<int>()));
        hps.Add(new HistoryPosition("年级排名", new List<string>(), new List<int>()));
        if (Common.CurrentVersion != EduVersion.School)
        {
            hps.Add(new HistoryPosition("统考排名", new List<string>(), new List<int>()));
        }
        Session["ChartData"+CommonScore.LogonStudent.RID] = hps;
        isubjID = Convert.ToInt32(subjID);
        Hashtable ht = new Hashtable();
        ht.Add("SubjectIDS", eduids);
        if (Convert.ToInt32(subjID) > 0)
        {
            ht.Add("SubjectID", Convert.ToInt32(subjID));
        }
        subjEdu = Container<Meqs.Logic.Interface.IExamSubjecyEducationLogic>.Instance.GetExamCause(ht);
        ht = new Hashtable();
        ht.Add("SchoolMark", CommonScore.GetSchoolMark);
        ht.Add("SubjectIDS", schoolids);
        if (Convert.ToInt32(subjID) > 0)
        {
            ht.Add("SubjectID", Convert.ToInt32(subjID));
        }
        subjSchool = Container<Meqs.Logic.Interface.IExamSubjecySchoolLogic>.Instance.GetExamCause(ht);
        string[] edids = eduids.Split(',');
        string[] edgs = edugrades.Split(',');
        string[] sids = schoolids.Split(',');

        List<StuExam> ses = new List<StuExam>();
        List<int> te = new List<int>();
        foreach(ExamSubjecyEducation2 ese in subjEdu)
        {
            if (!te.Contains(ese.ExamID))
            {
                StuExam s = new StuExam(1, ese.ExamID, "", 0, GetGrade(edids, edgs, ese.ExamID), 0, ese.CreateTime);
                ses.Add(s);
                te.Add(ese.ExamID);
            }
        }
        te = new List<int>();
        foreach (ExamSubjecySchool2 ese in subjSchool)
        {
            if (!te.Contains(ese.ExamID))
            {
                StuExam s = new StuExam(0, ese.ExamID, "", 0, 0, 0, ese.CreateTime);
                ses.Add(s);
                te.Add(ese.ExamID);
            }
        }
        ListSort<StuExam> sort = new ListSort<StuExam>(ses, "CreateTime", true);
        Repeater1.DataSource = sort.Sort();
        Repeater1.DataBind();
    }
    private int GetGrade(string[] examids,string [] grades,int examid)
    {
        int index = 0;
        foreach (string e in examids)
        {
            if (Convert.ToInt32(e) == examid)
            {
                return Convert.ToInt32(grades[index]);
            }
            index++;
        }
        return 0;
    }

    protected string GetExaminfo(int type, int examid, int grade)
    {
        string examinfo = ""; 
        if (type == 1)
        {

            ExamsEducation exam = Container<Meqs.Logic.Interface.IExamsEducationLogic>.Instance.FindById(examid);
            examinfo += "<td width='280px' style='text-align:left;padding-left: 5px;'>" + exam.Name + "</td>";

            Hashtable ht = new Hashtable();
            if (isubjID > 0)
            {
                ht.Add("ExamID", examid);
                ht.Add("StudentID", CommonScore.LogonStudent.RID);
                ht.Add("SubjectID", isubjID);
                ht.Add("TableName", CommonScore.GetGradeStoreTable(grade));
                IList<GradeScoreEducation> gs = Container<Meqs.Logic.Interface.IGradeScoreEducationLogic>.Instance.FindAll(ht);
                if (gs.Count > 0)
                {
                    int cp = 1;
                    int gp = 1;
                    int ep = 1;
                    ht = new Hashtable();
                    ht.Add("ClsID", gs[0].ClsID);
                    ht.Add("ExamID", examid);
                    ht.Add("Score", gs[0].Score !=null ? gs[0].Score :0);
                    ht.Add("SchoolMark", gs[0].SchoolMark);
                    ht.Add("SubjectID", isubjID);
                    ht.Add("TableName", CommonScore.GetGradeStoreTable(grade));
                    Container<Meqs.Logic.Interface.IGradeScoreEducationLogic>.Instance.GetPosition(ref cp, ref gp, ref ep, ht);
                    examinfo += "<td width='80px' style='text-align:left;padding-left: 5px;'>" + exam.CreateDate.ToString("yy-MM-dd") + "</td>";
                    examinfo += "<td width='80px' style='text-align:right;padding-right: 5px;'>" + cp + "</td>";
                    examinfo += "<td width='80px' style='text-align:right;padding-right: 5px;'>" + gp + "</td>";
                    if (Common.CurrentVersion != EduVersion.School)
                    {
                        examinfo += "<td width='80px' style='text-align:right;padding-right: 5px;'>" + ep + "</td>";
                    }
                    hps[0].ForX.Add(exam.CreateDate.ToString("yy-MM-dd"));
                    hps[1].ForX.Add(exam.CreateDate.ToString("yy-MM-dd"));
                    if (Common.CurrentVersion != EduVersion.School)
                    {
                        hps[2].ForX.Add(exam.CreateDate.ToString("yy-MM-dd"));
                    }
                    hps[0].ForY.Add(cp);
                    hps[1].ForY.Add(gp);
                    if (Common.CurrentVersion != EduVersion.School)
                    {
                        hps[2].ForY.Add(ep);
                    }
                }
            }
            else
            {
                ht.Add("ExamID", examid);
                ht.Add("StudentID", CommonScore.LogonStudent.RID);
                ht.Add("TableName", CommonScore.GetGradeStoreTable(grade));
                IList<GradeScoreEducation> gs = Container<Meqs.Logic.Interface.IGradeScoreEducationLogic>.Instance.FindAll(ht);
                double? fullscore = 0;
                foreach (GradeScoreEducation eg in gs)
                {
                    fullscore += eg.Score != null ? eg.Score : 0;
                }


                int cp = 1;
                int gp = 1;
                int ep = 1;
                ht = new Hashtable();
                ht.Add("ClsID", gs[0].ClsID);
                ht.Add("ExamID", examid);
                ht.Add("Score", fullscore);
                ht.Add("SchoolMark", gs[0].SchoolMark);
                ht.Add("TableName", CommonScore.GetGradeStoreTable(grade));
                Container<Meqs.Logic.Interface.IGradeScoreEducationLogic>.Instance.GetPosition(ref cp, ref gp, ref ep, ht);
                examinfo += "<td width='80px' style='text-align:left;padding-left: 5px;'>" + exam.CreateDate.ToString("yy-MM-dd") + "</td>";
                examinfo += "<td width='80px' style='text-align:right;padding-right: 5px;'>" + cp + "</td>";
                examinfo += "<td width='80px' style='text-align:right;padding-right: 5px;'>" + gp + "</td>";
                if (Common.CurrentVersion != EduVersion.School)
                {
                    examinfo += "<td width='80px' style='text-align:right;padding-right: 5px;'>" + ep + "</td>";
                }
                hps[0].ForX.Add(exam.CreateDate.ToString("yy-MM-dd"));
                hps[1].ForX.Add(exam.CreateDate.ToString("yy-MM-dd"));
                if (Common.CurrentVersion != EduVersion.School)
                {
                    hps[2].ForX.Add(exam.CreateDate.ToString("yy-MM-dd"));
                }
                hps[0].ForY.Add(cp);
                hps[1].ForY.Add(gp);
                if (Common.CurrentVersion != EduVersion.School)
                {
                    hps[2].ForY.Add(ep);
                }
            }

        }
        else
        {
            Hashtable ht = new Hashtable();
            ht.Add("RID", examid);
            ht.Add("SchoolMark", CommonScore.GetSchoolMark);
            IList<ExamsSchool> exam = Container<Meqs.Logic.Interface.IExamsSchoolLogic>.Instance.FindAll(ht);
            if (exam.Count > 0)
            {
                examinfo += "<td width='160px' style='text-align:left;padding-left: 5px;'>" + exam[0].Name + "</td>";
                examinfo += "<td width='80px' style='text-align:left;padding-left: 5px;'>" + exam[0].CreateDate.ToString("yy-MM-dd") + "</td>";
            }
            else
            {
                examinfo += "<td width='160px' style='text-align:left;padding-left: 5px;'></td>";
                examinfo += "<td width='160px' style='text-align:left;padding-left: 5px;'></td>";
            }
            ht = new Hashtable();
            if (isubjID > 0)
            {
                ht.Add("ExamID", examid);
                ht.Add("StudentID", CommonScore.LogonStudent.RID);
                ht.Add("SubjectID", isubjID);
                ht.Add("SchoolMark", CommonScore.GetSchoolMark);
                IList<ExamDetailSchool> gs = Container<Meqs.Logic.Interface.IExamDetailSchoolLogic>.Instance.FindAll(ht);
                if (gs.Count > 0)
                {
                    int cp = 1;
                    int gp = 1;
                    ht = new Hashtable();
                    ht.Add("ClsID", gs[0].ClsID);
                    ht.Add("ExamID", examid);
                    ht.Add("Score", gs[0].Score!=null ? gs[0].Score :0);
                    ht.Add("SchoolMark", CommonScore.GetSchoolMark);
                    ht.Add("SubjectID", isubjID);
                    Container<Meqs.Logic.Interface.IExamDetailSchoolLogic>.Instance.GetPosition(ref cp, ref gp, ht);

                    examinfo += "<td width='80px' style='text-align:right;padding-right: 5px;'>" + cp + "</td>";
                    examinfo += "<td width='80px' style='text-align:right;padding-right: 5px;'>" + gp + "</td>";
                    if (Common.CurrentVersion != EduVersion.School)
                    {
                        examinfo += "<td width='80px' style='text-align:right;padding-right: 5px;'></td>";
                    }
                    hps[0].ForX.Add(exam[0].CreateDate.ToString("yy-MM-dd"));
                    hps[1].ForX.Add(exam[0].CreateDate.ToString("yy-MM-dd"));
                    hps[0].ForY.Add(cp);
                    hps[1].ForY.Add(gp);
                }
            }
            else
            {
                ht.Add("ExamID", examid);
                ht.Add("StudentID", CommonScore.LogonStudent.RID);
                ht.Add("SchoolMark", CommonScore.GetSchoolMark);
                IList<ExamDetailSchool> gs = Container<Meqs.Logic.Interface.IExamDetailSchoolLogic>.Instance.FindAll(ht);
                double? fullscore = 0;
                foreach(ExamDetailSchool eg in gs)
                {
                    fullscore += eg.Score != null ? eg.Score : 0;
                }
                

                    int cp = 1;
                    int gp = 1;
                    ht = new Hashtable();
                    ht.Add("ClsID", gs[0].ClsID);
                    ht.Add("ExamID", examid);
                    ht.Add("Score", fullscore);
                    ht.Add("SchoolMark", CommonScore.GetSchoolMark);

                    Container<Meqs.Logic.Interface.IExamDetailSchoolLogic>.Instance.GetPosition(ref cp, ref gp, ht);

                    examinfo += "<td width='80px' style='text-align:right;padding-right: 5px;'>" + cp + "</td>";
                    examinfo += "<td width='80px' style='text-align:right;padding-right: 5px;'>" + gp + "</td>";
                    if (Common.CurrentVersion != EduVersion.School)
                    {
                        examinfo += "<td width='80px' style='text-align:right;padding-right: 5px;'></td>";
                    }
                    hps[0].ForX.Add(exam[0].CreateDate.ToString("yy-MM-dd"));
                    hps[1].ForX.Add(exam[0].CreateDate.ToString("yy-MM-dd"));
                    hps[0].ForY.Add(cp);
                    hps[1].ForY.Add(gp);

            }
            

        }
        
        return examinfo;
    }

    protected class StuExam
    {
        public StuExam(int t, int i, string n, int y, int g, int ct,DateTime d)
        {
            examType = t;
            examID = i;
            examName = n;
            schoolYear = y;
            grade = g;
            classType = ct;
            dateTime = d;
        }
        public int examType;
        public int examID;
        public string examName;
        public int schoolYear;
        public int grade;
        public int classType;
        public DateTime dateTime;

        public DateTime CreateTime
        {
            get { return dateTime; }
            set { dateTime = value; }
        }
        public int ExamType
        {
            get { return examType; }
            set { examType = value; }
        }
        public int ExamID
        {
            get { return examID; }
            set { examID = value; }
        }

        public string ExamName
        {
            get { return examName; }
            set { examName = value; }
        }

        public int SchoolYear
        {
            get { return schoolYear; }
            set { schoolYear = value; }
        }

        public int Grade
        {
            get { return grade; }
            set { grade = value; }
        }

        public int ClassType
        {
            get { return classType; }
            set { classType = value; }
        }
    }
}
